import urllib;
import urllib2;
import cookielib;
import Cookie;

from xml.etree import ElementTree;
from google.appengine.ext import db;
from google.appengine.api import urlfetch

class LatestID(db.Model):
    id = db.StringProperty();


def getLatestID():
    latestID = db.GqlQuery("SELECT * FROM LatestID ORDER BY id DESC LIMIT 10");
    cnt = latestID.count();
    if cnt != 0:
        return latestID[0].id;
    else:
        return "";

class URLOpener:
  def __init__(self):
      self.cookie = Cookie.SimpleCookie()
    
  def open(self, url, data = None):
      if data is None:
          method = urlfetch.GET
      else:
          method = urlfetch.POST
    
      while url is not None:
          response = urlfetch.fetch(url=url,
                          payload=data,
                          method=method,
                          headers=self._getHeaders(self.cookie),
                          allow_truncated=False,
                          follow_redirects=False,
                          deadline=10
                          )
          data = None # Next request will be a get, so no need to send the data again. 
          method = urlfetch.GET
          self.cookie.load(response.headers.get('set-cookie', '')) # Load the cookies from the response
          url = response.headers.get('location')
    
      return response
        
  def _getHeaders(self, cookie):
      headers = {
                 'Host' : 'www.google.com',
                 'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)',
                 'Cookie' : self._makeCookieHeader(cookie),
                 'Referer': 'http://t.sina.com.cn/'
                  }
      return headers

  def _makeCookieHeader(self, cookie):
      cookieHeader = ""
      for value in cookie.values():
          cookieHeader += "%s=%s; " % (value.key, value.value)
      return cookieHeader


def getXmlInTwitter(username,since_id=""):
    xmlUrl = "http://twitter.com/statuses/user_timeline/"+ username + ".xml";
    if since_id != "":
        xmlUrl = xmlUrl + "?since_id=" + since_id;
    print "parsing from " + xmlUrl + "<br>";
    opener = urllib2.build_opener();
    req = urllib2.Request(xmlUrl);
    return opener.open(req).read();


def synchronousMsg(twitterUsername, sinaUsername, sinaPassword):
    opener = URLOpener();
    loginParams = urllib.urlencode({"username":sinaUsername,
                                "password":sinaPassword,
                                "service":"miniblog",
                                "encoding":"utf-8"});
    res = opener.open("http://login.sina.com.cn/sso/login.php",loginParams);
    latestID = getLatestID();
    text = getXmlInTwitter(twitterUsername,latestID);
    #print text;
    lID = "";
    root = ElementTree.fromstring(text);
    statusNode = root.getiterator("status");
    for node in reversed(statusNode):
        tContent = node.find("text");
        tID = node.find("id");
        if tID != latestID:
            lID = tID.text.encode("utf-8");
            txt = tContent.text.encode("utf-8");
            print txt + "<br>";
            if txt[0:1] != '@':
                param = urllib.urlencode({"content":txt});
                res = opener.open("http://t.sina.com.cn/mblog/publish.php",param);
        
    if lID != "":
        ltID = LatestID();
        ltID.id = lID;
        ltID.put();
    
#========================================================
synchronousMsg(twitterUsername="twitter-id", sinaUsername="sina-id", sinaPassword="sina-password");
    
